<?php

namespace App\Http\Controllers\YApi;

//use Illuminate\Http\Request;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Session;
use Redirect;

use App\Models\Book;
use App\Models\Shudan;
use App\Models\UsersReg;

class ShudanlistController extends Controller
{

  public function shudanlist()
    {
        //书单列表
        $shudans = Shudan::orderBy('last_time', 'desc')->paginate(15);

        foreach ($shudans as $shudan) {
             //得到会员资料
            $shudan['users'] = UsersReg::find($shudan->uid);
        }

        foreach ($shudans as $shudan) {
            $shudanlist = $shudan->shudanlist;
            $arr = explode(',', $shudanlist);

            $shudan['geshu'] = count($arr); // 统计书单里书籍的数量

            //得到书单对应的书籍信息
            $c = 0;
            for($i=0; $i<count($arr); $i++){
                if($c < 3){ //显示书单里的前三个图书
                    $books[$i] = Book::select('bimg')->find($arr[$i]); 
                    $c = $c+1;
                }           
            }
            $shudan['shudanlists'] = $books; // 获得对应的图书信息            
        }
        return $shudans;
    }


    public function usershudanlist($uid)
    {
        //会员的书单列表
        $shudans = Shudan::where('uid', $uid)->orderBy('last_time', 'desc')->paginate(15);

        foreach ($shudans as $shudan) {
             //得到会员资料
            $shudan['users'] = UsersReg::find($shudan->uid);
        }

        foreach ($shudans as $shudan) {
            $shudanlist = $shudan->shudanlist;
            $arr = explode(',', $shudanlist);

            $shudan['geshu'] = count($arr); // 统计书单里书籍的数量

            //得到书单对应的书籍信息
            $c = 0;
            for($i=0; $i<count($arr); $i++){
                if($c < 3){ //显示书单里的前三个图书
                    $books[$i] = Book::select('bimg')->find($arr[$i]); 
                    $c = $c+1;
                }           
            }
            $shudan['shudanlists'] = $books; // 获得对应的图书信息            
        }
        return $shudans;
    }
    
}